//
//  ContentParseCenter.m
//  first app
//
//  Created by 林 小明 on 14-7-10.
//
//

#import "ContentParseCenter.h"
#import "FileCenter.h"

@implementation ContentParseCenter

+ (NSMutableArray*)parseContent:(NSString*)htmlContent
{
    NSMutableArray* titleIndexs = [[NSMutableArray alloc] initWithCapacity:20];
    
    NSMutableArray* chapterBlock = [self parseChapterBlock:htmlContent];
    if( chapterBlock != nil )
    {
        NSLog(@"chapterBlock size:%lu", (unsigned long)[chapterBlock count]);
        for ( NSDictionary* dict in chapterBlock )
        {
            NSMutableArray* sessionList = [NSMutableArray arrayWithCapacity:20];
            
            NSString* chapterTitle = [dict objectForKey:@"title"];
            //NSLog( @"chapterTitle:%@", chapterTitle );
            
            NSString* str = [dict objectForKey:@"listcontent"];
            NSMutableArray* sessionBlock = [self parseSessionBlock:str];
            if( sessionBlock!=nil )
            {
                for ( NSDictionary* sessDict in sessionBlock )
                {
                    NSString* sessionTitle = [sessDict objectForKey:@"title"];
                    //NSLog( @"sessionTitle:%@", sessionTitle );
                    
                    NSString* url = [sessDict objectForKey:@"url"];
                    //NSLog( @"url:%@", url );
                    
                    [sessionList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                            sessionTitle,@"sessionTitle",
                                            url,@"sessionURL",
                                            nil]];
                }
            }
            
            [titleIndexs addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                    chapterTitle,@"chapterTitle",
                                    sessionList,@"sessionList",
                                    nil]];
        }
    }
    
    return titleIndexs;
}

+ (NSMutableArray*)parseSessionBlock:(NSString*)content
{
    NSString *pattern = @"<h3 class=\"title\" style=\".*?\">(.*)<span class=\"time\"></span></h3>[\\s\\S]+?\'local\'\\)\" href=\"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)\">";
    
    NSMutableArray* array = nil;
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray *matches = [regex matchesInString:content options:0 range:NSMakeRange(0, content.length)];
    
    if (matches)
    {
        array = [[NSMutableArray alloc] initWithCapacity:20];
        for (NSTextCheckingResult *match in matches)
        {
            if ( match.numberOfRanges >= 3 )
            {
                NSString* title = [[NSString alloc]init];
                title = [content substringWithRange:[match rangeAtIndex:1]];
                
                NSString* url = [[NSString alloc]init];
                url = [content substringWithRange:[match rangeAtIndex:2]];
                
                [array addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                  title,@"title",
                                  url,@"url",
                                  nil]];
            }
        }
    }
    
    return array;
}

+ (NSMutableArray*)parseChapterBlock:(NSString*)content
{
    NSString *pattern = @"<h1 class=\"title\">(.+)</h1><span class=\"date\">(.+)</span>[\\s\\S]+?<ol class=\"download\\-list\">([\\s\\S]+?)</ol>";
    
    NSMutableArray* array = nil;
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray *matches = [regex matchesInString:content options:0 range:NSMakeRange(0, content.length)];
    
    if (matches)
    {
        array = [[NSMutableArray alloc] initWithCapacity:20];
        for (NSTextCheckingResult *match in matches)
        {
            if ( match.numberOfRanges >= 4 )
            {
                NSString* title = [[NSString alloc]init];
                title = [title stringByAppendingString:[content substringWithRange:[match rangeAtIndex:1]]];
                title = [title stringByAppendingString:[content substringWithRange:[match rangeAtIndex:2]]];
                
                NSString* str = [[NSString alloc]init];
                str = [content substringWithRange:[match rangeAtIndex:3]];
                
                [array addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                  title,@"title",
                                  str,@"listcontent",
                                  nil]];
            }
        }
    }
    
    return array;
}

@end
